import { toRef } from 'vue'
import { useQuery } from '@tanstack/vue-query'
import { ApiSearchClasses, useSearchClassesQueryKey } from '@/apis/class'
import { useUserStore } from '@/stores/user'
import { useDebounce } from '@vueuse/core'

export function useCourseList() {
  const user = toRef(useUserStore(), 'user')
  const { data: classes, isLoading } = useQuery({
    staleTime: 5 * 60_000,
    queryKey: useSearchClassesQueryKey({ student: user.value.username }),
    queryFn: ({ queryKey: [, , query] }) => ApiSearchClasses(query),
    select: ({ data }) => data
  })

  return {
    classes,
    /** 延迟更新, 确保骨架屏最少展示时间 */
    isLoading: useDebounce(isLoading, 800)
  }
}
